/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.vsu.cs.piit.vokod;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Галчонок
 */
public class ObservableArrayList<T> extends ArrayList<T>{
    ArrayList<IListListener> listeners = new ArrayList<IListListener>();

    public ObservableArrayList(List<T> list) {
        super(list);
    }

    public ObservableArrayList() {
        super();
    }

    public void AddListener(IListListener listener) {
        this.listeners.add(listener);
    }

    public void RemoveListener(IListListener listener) {
        this.listeners.remove(listener);
    }

    public void IsChanged(T object) {
        for (int i = 0; i < listeners.size(); i++) {
            listeners.get(i).doSave(object);
        }
    }

    public void IsRemove(T object) {
        for (int i = 0; i < listeners.size(); i++) {
            listeners.get(i).doRemove(object);
        }
    }

    @Override
    public boolean add(T e) {
        boolean res = super.add(e);
        IsChanged(e);
        return res;
    }

    @Override
    public void add(int index, T element) {
        super.add(index, element);
        IsChanged(element);
    }

    @Override
    public boolean remove(Object o) {
        boolean res = super.remove(o);
        IsRemove((T)o);
        return res;
    }

}
